<?php
class GoodsModel extends BaseModel {

	
	public $pagesize = 2;
	public function getGoodsList($keyword="",$cat_id=0,$brand_id=0,$page=1){
		$sql = "select * from ecs_goods where 1=1 ";
		$sql_count =  "select count(*) from ecs_goods where 1=1 ";
		if($keyword){
			$sql.=" and goods_name like '%".$keyword."%'" ; 
			 $sql_count.= " and goods_name like '%".$keyword."%'" ; 
		}
		if($cat_id){
			$sql.=  " and cat_id = ".$cat_id;
			$sql_count.=" and cat_id = ".$cat_id;
		}
		if($brand_id){
			$sql.= " and brand_id= ".$brand_id;
			$sql_count.=" and brand_id= ".$brand_id;
		}
		
		$offset = ($page-1)*$this->pagesize;

		$sql.=" limit ".$offset.",".$this->pagesize;
		
		$list = $this->db->getAll($sql);

	
		$total_record = $this->db->getOne($sql_count);
		
		foreach ($list as $key=>&$val){
			$brand_id = $val["brand_id"];
			$brand_name = (new BrandModel())->getBrandName($brand_id);
			$val["brand_name"] = $brand_name;

			$cat_id = $val["cat_id"];
			$sql = "select cat_name from ecs_category where cat_id=".$cat_id;
			$cat_name =  $this->db->getOne($sql);
			$val["cat_name"] = $cat_name;
			
		}
		
		return array("data"=>$list,"total"=>$total_record);

	}
	
}